package com.kupurui.asteacher.ui.login;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.frame.util.AppJsonUtil;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.http.Teacher;
import com.kupurui.asteacher.bean.UserInfo;
import com.kupurui.asteacher.ui.BaseAty;
import com.kupurui.asteacher.ui.MainActivity;
import com.kupurui.asteacher.ui.mine.AgreementAty;
import com.kupurui.asteacher.utils.UserManger;

import butterknife.Bind;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/7/17.
 */

public class LoginAty extends BaseAty {

    @Bind(R.id.et_account)
    EditText etAccount;
    @Bind(R.id.et_password)
    EditText etPassword;
    @Bind(R.id.tv_agreemment)
    TextView tvAgreemment;
    @Bind(R.id.tv_login)
    TextView tvLogin;
    @Bind(R.id.tv_find_pwd)
    TextView tvFindPwd;

    private String user = "";
    private String password = "";

    @Override
    public int getLayoutId() {
        return R.layout.login_aty;
    }

    @Override
    public void initData() {
        if (UserManger.isLogin()){
            startActivity(MainActivity.class,null);
            finish();
        }

        String agreement = "《奥数用户协议》";
        String str = "登录即表示您已阅读并同意" + agreement;
        SpannableString spanableInfo = new SpannableString(str);
        spanableInfo.setSpan(new Clickable(), str.lastIndexOf(agreement), str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvAgreemment.setText(spanableInfo);
        tvAgreemment.setMovementMethod(LinkMovementMethod.getInstance());
    }

    @Override
    public void requestData() {

    }

    @OnClick({R.id.tv_login,R.id.tv_find_pwd,R.id.tv_agreemment})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.tv_login:
                toLogin();
                break;
            case R.id.tv_find_pwd:
                startActivity(FindPwdAty.class,null);
                break;
            case R.id.tv_agreemment:
                startActivity(AgreementAty.class,null);
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what){
            case 0:
                UserInfo userInfo = AppJsonUtil.getObject(result,UserInfo.class);
                UserManger.setUserInfo(userInfo);
                UserManger.setIsLogin(true);
                startActivity(MainActivity.class,null);
                finish();
                break;
        }
        super.onSuccess(result, call, response, what);
    }

    private void toLogin(){
        user = etAccount.getText().toString();
        password = etPassword.getText().toString();
        if (TextUtils.isEmpty(user)){
            showToast("请输入登录账号");
            return;
        }
        if (TextUtils.isEmpty(password)){
            showToast("请输入登录账号");
            return;
        }
        showLoadingDialog("");
        new Teacher().login_2(user,password,this,0);
    }


    class Clickable extends ClickableSpan {

        /**
         * 重写父类点击事件
         */
        @Override
        public void onClick(View v) {
//            startActivity(AgreementAty.class, null);
        }


        /**
         * 重写父类updateDrawState方法  我们可以给TextView设置字体颜色,背景颜色等等...
         */
        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(getResources().getColor(R.color.tab_color));
//            ds.setColor(ContextCompat.getColor(LoginAty.this,R.color.tab_color));
        }
    }
}
